package com.ruoyi.project.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.ResponseResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.blog.domain.BTags;
import com.ruoyi.project.blog.domain.vo.SystemTagListVo;
import com.ruoyi.project.blog.service.IBTagsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 博客标签Controller
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RestController
@RequestMapping("/articles/tags")
public class BTagsController extends BaseController {
    @Autowired
    private IBTagsService bTagsService;

    @Anonymous
    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ApiOperation(value = "标签列表", httpMethod = "GET", response = ResponseResult.class, notes = "标签列表")
    public ResponseResult selectTagList(Long userId) {
        return bTagsService.selectTagList(userId);
    }

    /**
     * 查询博客标签列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BTags bTags) {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if (!admin) {
            bTags.setUserId(SecurityUtils.getUserId().toString());
        }
        startPage();
        List<SystemTagListVo> list = bTagsService.selectBTagsList(bTags);
        return getDataTable(list);
    }

    /**
     * 导出博客标签列表
     */
    @Log(title = "标签管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BTags bTags) {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if (!admin) {
            bTags.setUserId(SecurityUtils.getUserId().toString());
        }
        List<SystemTagListVo> list = bTagsService.selectBTagsList(bTags);
        ExcelUtil<SystemTagListVo> util = new ExcelUtil<SystemTagListVo>(SystemTagListVo.class);
        util.exportExcel(response, list, "博客标签数据");
    }

    /**
     * 获取博客标签详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bTagsService.selectBTagsById(id));
    }

    /**
     * 新增博客标签
     */
    @Log(title = "标签管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BTags bTags) {
        bTags.setUserId(SecurityUtils.getUserId().toString());
        return bTagsService.insertBTags(bTags);
    }

    /**
     * 修改博客标签
     */
    @Log(title = "标签管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BTags bTags) {
        return bTagsService.updateBTags(bTags);
    }


    /**
     * 博客标签
     */
    @Log(title = "标签管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bTagsService.deleteBTagsByIds(ids));
    }

    @GetMapping("/listNoPage")
    public AjaxResult listNoPage(String name) {
        List<SystemTagListVo> list = bTagsService.listTags(name);
        return AjaxResult.success(list);
    }
}
